﻿--@fn: GetMaterialThumbnail : 获取材质的预览图
--@params :mMatIdx  材质编辑器的插槽索引，从0开始
--@params : mSize 预览图的大小   例如：(dotnetclass "Autodesk.Max.PostageStampSize").Large
-- @return : 返回的bitmap
-- @ examples ： thumbnail = GetMaterialThumbnail 0 (dotnetclass "Autodesk.Max.PostageStampSize").Large
  fn GetMaterialThumbnail mMatIdx mSize =
   	(
   		iGlobal = (dotnetClass "Autodesk.Max.GlobalInterface").Instance
   		if iGlobal != undefined do
   		(
   			iMaterial = iGlobal.CoreInterface.GetMtlSlot mMatIdx
   			
   			if iMaterial != undefined do
   			(
   				pStamp = iMaterial.CreatePStamp mSize true
   				bytes = pStamp.Image
   				size = pStamp.Width
   				
   				bm = bitmap size size
   	
   				step = size*3
   				for y = 1 to bytes.count by step do
   				(
   					row = for x = y to (y+step-1) by 3 collect [bytes[x+2], bytes[x+1], bytes[x]]
   					setpixels bm [0, size-=1] row
   				)
   				pStamp.Dispose()
   				iMaterial.Dispose()
   			)
   		)
   		return bm
   	)